﻿using System;
using System.Collections.Generic;
using EchoFramework;

/// <summary>
/// 特殊参数表
/// </summary>
public class GlobalParaCfg : Config
{
    /// <summary>
    /// 关键词
    /// </summary>
    [ConfigMark]
    public readonly string GlobalKey;

    /// <summary>
    /// 内容
    /// </summary>
    [ConfigMark]
    public readonly string Content;

    /// <summary>
    /// 记录表内所有参数的字典
    /// key: GlobalKey
    /// value: Content
    /// </summary>
    private static Dictionary<string, string> paraDict;

    /// <summary>
    /// 返回特殊参数 string类型
    /// </summary>
    /// <param name="globalKey">关键词</param>
    private static string GetParaStr(string globalKey)
    {
        if (paraDict == null)
        {
            paraDict = new Dictionary<string, string>();
            List<GlobalParaCfg> cfgList = ConfigManager<GlobalParaCfg>.Instance.GetDataList();
            foreach (GlobalParaCfg cfg in cfgList)
            {
                if (paraDict.ContainsKey(cfg.GlobalKey))
                {
                    GameLogger.LogError($"已存在GlobalKey: {cfg.GlobalKey}");
                    continue;
                }

                paraDict.Add(cfg.GlobalKey, cfg.Content);
            }
        }

        if (!paraDict.ContainsKey(globalKey))
        {
            GameLogger.LogError($"获取特殊参数失败！不存在GlobalKey: {globalKey}");
            return string.Empty;
        }

        return paraDict[globalKey];
    }

    /// <summary>
    /// 返回特殊参数 int类型
    /// </summary>
    /// <param name="globalKey">关键词</param>
    public static int GetParaInt(string globalKey)
    {
        string paraStr = GetParaStr(globalKey);
        if (string.IsNullOrEmpty(paraStr))
        {
            return -1;
        }

        return Convert.ToInt32(paraStr);
    }
    
    /// <summary>
    /// 返回特殊参数 float类型
    /// </summary>
    /// <param name="globalKey">关键词</param>
    public static float GetParaFloat(string globalKey)
    {
        string paraStr = GetParaStr(globalKey);
        if (string.IsNullOrEmpty(paraStr))
        {
            return -1;
        }

        return Convert.ToSingle(paraStr);
    }
}